﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolCode.DataSource;

namespace CoolCode.Account
{
   public  class UserRole
    { 
        public bool CheckDuplicate(string id)
        {
            using (IDataProvider db = DBFactory.CreateInstance())
            {
                return db.Contains<IUser>(c => c.ID == id.ToLower());
            }
        }
 
        public IUser CheckAccess(string id, string pwd)
        {
            using (IDataProvider db = DBFactory.CreateInstance())
            {
                var q = db.Query<IUser>(c => (c.ID == id && c.PassWord == pwd));
                if (q.Count > 0)
                    return q.First();
                return null;
            }
        }

        public AccessType  CheckAccess(IUser user, string pageID)
        {
            using (IDataProvider db = DBFactory.CreateInstance())
            {
                var result = from q in db.Query<IAccess>(c => (c.PageID == pageID && c.Owner == user.ID)) select q.Type;
                return result.Single();
            }
        }

        public bool Regedit(IUser user)
        {
            using (IDataProvider db = DBFactory.CreateInstance())
            {
                if (db.Contains<IUser>(c => c.ID == user.ID))
                    return false;
                db.Store(user);
                return true;
            }
        }

        public IList<IUser> GetIUsers()
        {
            using (IDataProvider db = DBFactory.CreateInstance())
            {
                return db.Query<IUser>();
            }
        }
    }
}
